import 'package:dio/dio.dart';
import 'http_exception.dart';
// import 'package:flutter/foundation.dart';
// import 'dart:convert';
// import 'package:logger/logger.dart';

class HttpInterceptor extends Interceptor {
  //请求拦截
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    // TODO: implement onRequest
    super.onRequest(options, handler);
  }

  //响应拦截
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    // if (kDebugMode) {
    //   Logger().d("********** Response **********\n"
    //       "time: ${DateTime.now()}\n"
    //       "url: ${response.requestOptions.path.contains("://") ? "" : response.requestOptions.baseUrl}${response.requestOptions.path}\n"
    //       "statusMessage: ${response.statusMessage}\n"
    //       "statusCode: ${response.statusCode}\n"
    //       "data: ${prettyJson((response.data is String) ? jsonDecode(response.data) : response.data)}");
    // }

    // TODO: implement onResponse
    super.onResponse(response, handler);
  }

  //异常拦截
  @override
  // ignore: deprecated_member_use
  void onError(DioError err, ErrorInterceptorHandler handler) {
    // TODO: implement onError
    HttpException httpException = HttpException.create(err);
    print(httpException.msg);
    super.onError(err, handler);
  }
}
